package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.OrganizationPageDTO;
import com.cheer.entity.Organization;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Mapper
@Repository
public interface OrganizationDao extends BaseMapper<Organization> {

    default Organization getByParentOrgId(Long parentOrgId) {
        return selectOne(new QueryWrapper<Organization>()
                .eq("parent_org_id", parentOrgId)
                .last("limit 1"));
    }

    default List<Organization> listByParentOrgId(Long orgId) {
        return selectList(new QueryWrapper<Organization>()
                .eq("parent_org_id", orgId));
    }

    default Organization getByLevel(Integer level) {
        return selectOne(new QueryWrapper<Organization>()
                .eq("level", level)
                .last("limit 1"));
    }

    default List<Organization> listByOrgPath(String orgPath) {
        return selectList(new QueryWrapper<Organization>()
                .likeRight("org_path", orgPath));
    }

    default IPage<Organization> listPageByOrgPath(Integer pageNum, Integer pageSize, String orgPath) {
        return selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<Organization>()
                .likeRight("org_path", orgPath));
    }

    default List<Organization> listAll() {
        return selectList(new QueryWrapper<>());
    }

    List<OrganizationPageDTO> listPage(@Param("orgPath") String orgPath, @Param("childLevel")Integer childLevel, @Param("currenttLevel")Integer currenttLevel);


    default List<Organization> listByTypeAndLevel(Integer type, Integer cityLevel){

        QueryWrapper<Organization> queryWrapper = new QueryWrapper<Organization>();
        queryWrapper.lambda().eq(Organization::getOrgType, type).eq(Organization::getLevel, cityLevel);
        return selectList(queryWrapper);
    }

    default List<Organization> listByOrgPathAndRole(String orgPath, List<Integer> orgRoles){

        QueryWrapper<Organization> queryWrapper = new QueryWrapper<Organization>();
        queryWrapper.lambda().likeRight(Organization::getOrgPath, orgPath).in(Organization::getOrgRole, orgRoles);
        return selectList(queryWrapper);
    }

    default List<Organization> listByOrgRole(ArrayList<Integer> roles){

        QueryWrapper<Organization> queryWrapper = new QueryWrapper<Organization>();
        queryWrapper.lambda().in(Organization::getOrgRole, roles);
        return selectList(queryWrapper);
    }

}
